package com.feng.api;

import com.feng.api.client.OrderRemoteClient;
import com.feng.api.vo.UserVo;
import com.feng.service.IUserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xiaoleilu.hutool.json.JSONUtil;
import io.micrometer.core.instrument.util.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import nonapi.io.github.classgraph.json.JSONUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Api(value = "UserController", tags = "User API测试接口")
@RestController
public class UserController implements ApplicationContextAware {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderRemoteClient orderRemoteClient;

    @Autowired
    private IUserService userService;

    @ApiOperation("user info")
    @GetMapping("/user/info")
    public String userInfo() {
        return "zhangfeng";
    }


    @ApiOperation("order info")
    @GetMapping("/user/order")
    public String userOrder() {
        String orderInfo = restTemplate.getForObject("http://localhost:7001/order/info", String.class);
        log.info("获取订单信息:{}",orderInfo);
        return "zhangfeng orderInfo:" + orderInfo;
    }

    @ApiOperation("order info loadbalance")
    @GetMapping("/user/order/loadbalance")
    public String userOrderLoadBalance() {
        String orderInfo = restTemplate.getForObject("http://order-server/order/info", String.class);
        log.info("获取订单信息:{}",orderInfo);
        return "zhangfeng orderInfo:" + orderInfo;
    }

    @ApiOperation("order info openfeign")
    @GetMapping("/user/order/openfeign")
//    @HystrixCommand(fallbackMethod = "defaultCallHello")
    public String userOrderOpenfeign() {
        String orderInfo = orderRemoteClient.orderInfo();
        log.info("获取订单信息:{}",orderInfo);
        return "zhangfeng orderInfo:" + orderInfo;
    }

    public String defaultCallHello() {
        return "method fail";
    }

    @ApiOperation("add user")
    @PostMapping("/user/add")
//    @HystrixCommand(fallbackMethod = "defaultCallHello")
    public String addUser(@RequestHeader("Authorization") String token, @RequestBody UserVo userVo) {
        userService.addUser(userVo);
        final String s = JSONUtil.toJsonStr(userVo);
        log.info("user token:{},\ninfo:{}",token,s);
        return "success";
    }

    @ApiOperation("update user")
    @PutMapping("/user/update")
    @HystrixCommand(fallbackMethod = "defaultCallHello",commandProperties = {
        @HystrixProperty(name="execution.isolation.strategy", value = "THREAD")
    })
    public String updateUser(@RequestHeader("Authorization") String token, @ModelAttribute UserVo userVo) {

        final String s = JSONUtil.toJsonStr(userVo);
        log.info("user token:{},\ninfo:{}",token,s);
        return "success";
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("controller applicationContext is :{}",applicationContext.getClass().getName());
    }
}